<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class daftar_ulang extends CI_Controller {
	
	var $objectid = 'daftar_ulang';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/psb/mdl_daftar_ulang');
		$this->load->model('sd/psb/mdl_calon_siswa');
		//$this->output->enable_profiler(TRUE);
	}
	
	function add($id){
		$data['objectid'] = $this->objectid;
		
		$r = $this->mdl_daftar_ulang->get_calonsiswa_by_id($id);
		$nis = $this->mdl_daftar_ulang->generate_nis();
		
		$data['id'] = $id;
		$data['no_pendaftaran'] = $r->row()->no_pendaftaran;
		$data['nama'] = $r->row()->nama;
		$data['nis'] = $nis;
		
		$data['pembayaran'] = $this->mdl_daftar_ulang->get_setting_pembayaran();
		$data['uang_sumbangan'] = $r->row()->uang_sumbangan;
		
		$this->load->view('sd/psb/daftar_ulang_form', $data);
	}
	
	function save(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules('nis', 'NIS', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi dengan angka.');
		$this->form_validation->set_message('email', 'Field %s harus diisi dengan alamat email yang benar.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('nis',' ',' '))==''?'':form_error('nis',' ',' ').'<br>';
			
		}else{
			$result = !$this->mdl_daftar_ulang->nis_exist($data['nis']);
			if($result){
				$data['calonsiswa'] = $this->mdl_daftar_ulang->get_calon_siswa($data['id']); 
				$result = $this->mdl_daftar_ulang->save($data);
			}else{
				$data["pesan_error"] = 'Sudah terdapat nis dalam database, ganti dengan yang lain.';
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true, 'id'=>$data['id']));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function generate_nis(){
		echo $this->mdl_daftar_ulang->generate_nis();
	}
	
	function print_bukti($id){ // print bukti pendaftaran
		$this->load->library('HTML2PDF');
		$html2pdf = new HTML2PDF('P', 'A4', 'fr');
		$html2pdf->setDefaultFont('Arial');
		
		//$data['nama'] = 'indriyanto';
		$data['nama_sekolah'] = 'SD NEGERI SUMBER SARI INDAH';
		$data['alamat_sekolah'] = 'Jln. Sumber Makmur No.32, Telp.(0967)581562';
		
		// ambil data dari tabel
		$data['res'] = $this->mdl_daftar_ulang->get_calonsiswa_by_id($id);
		$data['pembayaran'] = $this->mdl_daftar_ulang->get_setting_pembayaran();
		$data['uang_sumbangan'] = $data['res']->row()->uang_sumbangan;
		
		$konten = $this->load->view('sd/psb/daftar_ulang_rpt', $data, true);
		
		$html2pdf->writeHTML($konten, false);
		
		$html2pdf->Output('bukti_pendaftaran.pdf');
	}
	
}